package com.yuxl.common.auth.service;

import com.yuxl.common.auth.entity.AuthPermission;
import com.yuxl.common.auth.repository.AuthPermissionRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

/**
 * @Author : yuxl
 * @create 2025/7/22 下午5:09
 */
@Slf4j
@Service
public class AuthPermissionService {
    @Resource
    private AuthPermissionRepository authPermissionRepository;

    public Flux<AuthPermission> findAll() {
        return authPermissionRepository.findAll();
    }

    public Mono<AuthPermission> findByMark(String mark) {
        return authPermissionRepository.findById(mark)
                .defaultIfEmpty(new AuthPermission());
    }

    public Mono<Void> deleteByMark(String mark) {
        return authPermissionRepository.deleteById(mark);
    }

    public Mono<AuthPermission> create(AuthPermission user) {
        user.setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        return authPermissionRepository.save(user);
    }

    public Mono<Integer> count(AuthPermission user) {
        return authPermissionRepository.count(user);
    }


    public Flux<AuthPermission> page(AuthPermission user, int page, int size) {
        long offset = (long) (page - 1) * size;
        return authPermissionRepository.page(user, size, offset);
    }


    public Flux<AuthPermission> list(AuthPermission user) {
        return authPermissionRepository.list(user);
    }

    public Mono<AuthPermission> updateById(AuthPermission user) {
        return authPermissionRepository.updateById(user);
    }
}
